class Solution(object):
    def reversePairs(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        
        def merge(l,r):
            if l >= r:
                return 0
            count = 0   
            mid = (l + r) // 2
            count = merge(l,mid) + merge(mid+1,r)

            # 计算重要逆序对
            i, j = l, mid + 1
            while i <= mid and j <= r:
                if nums[i] > 2 * nums[j]:
                    count += mid - i + 1
                    j += 1
                else:
                    i += 1

            # 合并排序
            i, j = l, mid + 1
            temp = []
            while i <= mid and j <= r:
                if nums[i] <= nums[j]:
                    temp.append(nums[i])
                    i += 1
                else:
                    temp.append(nums[j])
                    j += 1
            temp.extend(nums[i:mid+1])
            temp.extend(nums[j:r+1])
            nums[l:r+1] = temp
            
            return count 

        return merge(0,len(nums)-1) 


if __name__ == "__main__":
    nums = [-5, -3, -1, 0, 2]
    print(Solution().reversePairs(nums))
    print("排序后数组：", nums)
